﻿/*
 * doctor的相关的调度
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WH.NurseSys.Model;
using WH.NurseSys.BLL;

namespace WH.NurseSys.Web.Controllers
{
    public class DoctorController : Controller
    {
        //登录展示页
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        //登录业务逻辑处理
        [HttpPost]
        public ActionResult Login(string id)
        {
            string username = HttpContext.Request.Form["username"];
            string password = HttpContext.Request.Form["password"];
            string save = HttpContext.Request.Form["remember"];
            if (string.IsNullOrWhiteSpace(username))
            {
                ViewBag.Message = "用户名不能为空";
                return View("~/views/doctor/login.cshtml");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                ViewBag.Message = "密码不能为空";
                return View("~/views/doctor/login.cshtml");
            }
            int code;
            Doctor doctor = new DoctorBLL().Login(username, password, out code);
            if (code == 1)
            {
                Session["doctor"] = doctor;
                if (!string.IsNullOrWhiteSpace(save))
                {
                    HttpCookie cookieDoctor = new HttpCookie("doctor");
                    cookieDoctor.Values["username"] = username;
                    cookieDoctor.Values["password"] = password;
                    cookieDoctor.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(cookieDoctor);
                }
                return Redirect("/patient/list");
            }
            else if (code == 2)
            {
                ViewBag.Message = "帐号错误";
                return View("~/views/doctor/login.cshtml");
            }
            else if (code == 3)
            {
                ViewBag.Message = "密码";
                return View("~/views/doctor/login.cshtml");
            }
            else
            {
                ViewBag.Message = "登录异常，请联系管理员";
                return View("~/views/doctor/login.cshtml");
            }
        }

        //登出
        public ActionResult Logout()
        {
            Session["doctor"] = null;
            HttpCookie cookieDoctor = new HttpCookie("doctor");
            cookieDoctor.Expires = DateTime.Now.AddDays(-100);
            Response.Cookies.Add(cookieDoctor);
            return Redirect("/");
        }

        //注册帐号
        [HttpGet]
        public ActionResult Insert()
        {
            List<Dept> deptList = new DeptBLL().GetList();
            ViewBag.DeptList = deptList;
            return View();
        }

        //注册doctor帐号，提交信息
        [HttpPost]
        public ActionResult Insert(string id)
        {
            DoctorBLL doctorBLL = new DoctorBLL();
            string username = Request.Form["username"];
            if (string.IsNullOrWhiteSpace(username))
            {
                List<Dept> deptList = new DeptBLL().GetList();
                ViewBag.DeptList = deptList;
                ViewBag.Tips = "该用户名无效";
                return View();
            }
            if (doctorBLL.Exist(username))
            {
                List<Dept> deptList = new DeptBLL().GetList();
                ViewBag.DeptList = deptList;
                ViewBag.Tips = "用户名已存在";
                return View();
            }
            string nickname = Request.Form["nickname"];
            if (string.IsNullOrWhiteSpace(nickname))
            {
                List<Dept> deptList = new DeptBLL().GetList();
                ViewBag.DeptList = deptList;
                ViewBag.Tips = "昵称无效";
                return View();
            }
            string password1 = Request.Form["password1"];
            string password2 = Request.Form["password2"];
            if (string.IsNullOrWhiteSpace(password1) || !password1.Equals(password2))
            {
                List<Dept> deptList = new DeptBLL().GetList();
                ViewBag.DeptList = deptList;
                ViewBag.Tips = "两次输入的密码不相同";
                return View();
            }
            string deptIdText = Request.Form["deptId"];
            Doctor doctor = new Doctor();
            doctor.Id = Guid.NewGuid();
            doctor.Username = username;
            doctor.Nickname = nickname;
            doctor.Pwd = password1;
            doctor.Rank = 1;
            doctor.AddDate = DateTime.Now;
            doctor.DeptId = Guid.Parse(deptIdText);
            doctorBLL.Insert(doctor);
            Session["doctor"] = doctor;
            return Redirect("/doctor/info/"+doctor.Id.ToString());
        }

        //查看用户信息
        public ActionResult Info(string id)
        {
            if (Session["doctor"] == null)
            {
                return Redirect("/doctor/login");
            }
            if (string.IsNullOrWhiteSpace(id))
            {
                Doctor doctor = Session["doctor"] as Doctor;
                doctor = new DoctorBLL().GetById(doctor.Id);
                return View(doctor);
            }
            else
            {
                Guid uid = Guid.Parse(id);
                Doctor doctor = new DoctorBLL().GetById(uid);
                return View(doctor);
            }
        }
    }
}
